package com.example.testapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testapplication.R;
import com.example.testapplication.entity.CartEntity;
import com.example.testapplication.entity.ShoppingMsgEntity;

import java.util.List;

public class CartFragMentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;
    private List<CartEntity> listDatas;
    public void setListDatas(List<CartEntity> listDatas) {
        this.listDatas = listDatas;
    }

    public CartFragMentAdapter(Context context) {
        this.mContext = context;
    }
    public CartFragMentAdapter(Context context, List<CartEntity> datas) {
        this.listDatas=datas;
        this.mContext = context;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_cart,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        CartEntity cartEntity = listDatas.get(position);
        vh.tvUserName.setText(cartEntity.getUsername());
        vh.tvGoodName.setText(cartEntity.getGoodname());
        vh.tvNum.setText(String.valueOf(cartEntity.getNum()));
        vh.tvPrice.setText(String.valueOf(cartEntity.getPrice()));
    }

    @Override
    public int getItemCount() {
        if(listDatas!=null&&listDatas.size()>0)
        {
            return listDatas.size();
        }else{
            return 0;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvGoodName;
        private TextView tvPrice;
        private TextView tvUserName;
        private TextView tvNum;
        public ViewHolder(@NonNull View view) {
            super(view);
            tvNum=view.findViewById(R.id.cart_goodnum);
            tvPrice=view.findViewById(R.id.cart_goodprice);
            tvGoodName=view.findViewById(R.id.cart_goodname);
            tvUserName=view.findViewById(R.id.cart_username);
        }
    }
}
